package com.onetech.adrewards.free.activities;

import android.content.Intent;
import android.graphics.Color;
import android.os.AsyncTask;
import android.os.AsyncTask.Status;
import android.os.Build;
import android.os.Bundle;
import android.os.StrictMode;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBarActivity;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import com.onetech.adrewards.free.R;
import com.onetech.adrewards.free.R.id;
import com.onetech.adrewards.free.config.AppConfig;
import com.onetech.adrewards.free.config.AppConfig.Cache;
import com.onetech.adrewards.free.dialogs.FeedbackDialog;
import com.onetech.adrewards.free.dialogs.GuideDialogFragment;
import com.onetech.adrewards.free.dialogs.LoginBonusDialog;
import com.onetech.adrewards.free.dialogs.v2.ExitAppDialog;
import com.onetech.adrewards.free.fragments.v2.HomeFragment;
import com.onetech.adrewards.free.fragments.v2.NavigationDrawerFragment;
import com.onetech.adrewards.free.models.CommonModel;
import com.onetech.adrewards.free.models.UserModel;
import com.onetech.adrewards.free.objects.UserInfoDTO;
import com.onetech.adrewards.free.utils.AdrewadsUtils;

public class BaseNewActivity extends ActionBarActivity implements
		NavigationDrawerFragment.NavigationDrawerCallbacks, OnClickListener,
		OnPageChangeListener {
	private NavigationDrawerFragment mNavigationDrawerFragment;

	private CharSequence mTitle;
	private ActionBar mActionbar;

	private TextView mAppTitle = null;
	private ImageView mHomeBanner = null;
	private ProgressBar mCoinProgressBar = null;
	private TextView mMyCoins = null;
	private TextView mMaxCoins = null;
	private View mQuestionBtn = null;

	private View mFooterView = null;
	private View mMyPointContainer = null;
	private View mContentProgressbarCoins = null;

	private final int MIN_COIN = 0;
	protected int mUserCoins = 0;
	// public int currentView = R.id.menuMiddle;

	protected GetUserInfo mGetUserInfo = null;
	private ImageView mNaviMenu = null;

	public interface ExitHandler {
		public void doExit();
	}

	private ExitHandler mHandler = null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD_MR1) {
			StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
					.permitAll().build());
		}
		setContentView(R.layout.activity_home);

		mNavigationDrawerFragment = (NavigationDrawerFragment) getSupportFragmentManager()
				.findFragmentById(R.id.navigation_drawer);
		mTitle = getTitle();

		// Set up the drawer.
		mNavigationDrawerFragment.setUp(R.id.navigation_drawer,
				(DrawerLayout) findViewById(R.id.drawer_layout));

		mActionbar = getSupportActionBar();
		mActionbar.hide();
		initView();
		initListener();

		mHandler = new ExitHandler() {
			@Override
			public void doExit() {
				finish();
			}
		};
	}

	public void switchViewTab() {
		FragmentManager fragmentManager = getSupportFragmentManager();
		HomeFragment fragment = (HomeFragment) fragmentManager
				.findFragmentById(R.id.container);
		if (fragment != null) {
			fragment.switchToTabView(1, 1);
			return;
		}
	}

	public void go2SNS() {
		Intent mSocialShareIntent = new Intent(this, SocialShareActivity.class);
		startActivity(mSocialShareIntent);
	}

	public void go2InviteFriends() {
		Intent mInviteFriends=new Intent(this, InviteFriendsActivity.class);
		startActivity(mInviteFriends);
	}

	@Override
	public void onNavigationDrawerItemSelected(int position) {
		switch (position) {
		case 5:
			go2InviteFriends();
			return;	
		case 6:
			go2SNS();
			return;
		default:
			break;
		}
		// update the main content by replacing fragments
		FragmentManager fragmentManager = getSupportFragmentManager();
		HomeFragment fragment = (HomeFragment) fragmentManager
				.findFragmentById(R.id.container);
		if (fragment != null) {
			if (position < 8) {
				if(position==7){
					fragment.switchToTabView(0, -1);
				}else{
					fragment.switchToTabView(1, position - 1);	
				}				
			} else {
				if (position == 8) {
					fragment.switchToTabView(2, 1);
					return;
				}
				if (position == 9) {
					fragment.switchToTabView(2, 3);
					return;
				}
				if (position == 10) {
					fragment.switchToTabView(2, 2);
					return;
				}
			}
			return;
		}
		Fragment mMainView = null;
		switch (position) {
		default:
			mMainView = HomeFragment.instantiate(getApplicationContext(),
					HomeFragment.class.getName());
			break;
		}
		fragmentManager.beginTransaction().replace(R.id.container, mMainView)
				.commit();
	}

	public void onSectionAttached(int number) {
		switch (number) {
		case 1:
			mTitle = getString(R.string.title_section1);
			break;
		case 2:
			mTitle = getString(R.string.title_section2);
			break;
		case 3:
			mTitle = getString(R.string.title_section3);
			break;
		}
	}

	public void restoreActionBar() {
		ActionBar actionBar = getSupportActionBar();
		actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
		actionBar.setDisplayShowTitleEnabled(true);
		actionBar.setTitle(mTitle);
	}

	// Base functions
	private void initView() {
		// mViewContainer = (LinearLayout)
		// this.findViewById(R.id.mViewContainer);
		mNaviMenu = (ImageView) findViewById(R.id.naviMenuIcon);
		mNaviMenu.setVisibility(View.VISIBLE);
		mFooterView = (LinearLayout) this.findViewById(R.id.appFooter);

		// menuBottomLeft = (LinearLayout) this.findViewById(R.id.menuLeft);
		// menuBottomMiddle = (LinearLayout) this.findViewById(R.id.menuMiddle);
		// menuBottomRight = (LinearLayout) this.findViewById(R.id.menuRight);
		mHomeBanner = (ImageView) this.findViewById(R.id.homeBanner);
		mCoinProgressBar = (ProgressBar) this
				.findViewById(R.id.myProgressBarPoint);
		mCoinProgressBar.setMax(AppConfig.MIN_COIN_EXCHANGE);
		mMyPointContainer = (LinearLayout) this.findViewById(R.id.myPoints);
		mContentProgressbarCoins = (LinearLayout) this
				.findViewById(R.id.myContentProgressBarCoins);

		mAppTitle = (TextView) this.findViewById(R.id.appTitle);
		mMyCoins = (TextView) this.findViewById(R.id.myCoins);
		mMaxCoins = (TextView) this.findViewById(R.id.maxCoins);
		mQuestionBtn = (LinearLayout) this.findViewById(R.id.appQuestion);
		// setDefaultMenuBackground();

		/*
		 * mFirstMenu = new QuickAction(this);
		 * mFirstMenu.setOnActionItemClickListener(this);
		 * setQuickAction(mFirstMenu, R.drawable.ic_launcher);
		 */
	}

	protected void setAppTittle(String title) {
		mAppTitle.setText(title);
	}

	private void initListener() {
		mHomeBanner.setOnClickListener(this);
		mContentProgressbarCoins.setOnClickListener(this);
		// menuBottomLeft.setOnClickListener(this);
		// menuBottomMiddle.setOnClickListener(this);
		// menuBottomRight.setOnClickListener(this);
		mQuestionBtn.setOnClickListener(this);
		mNaviMenu.setOnClickListener(this);
	}

	protected void showFooterView(boolean isShow) {
		mFooterView.setVisibility(isShow ? View.VISIBLE : View.GONE);
	}

	protected void showMyPoints(boolean isShow) {
		mMyPointContainer.setVisibility(isShow ? View.VISIBLE : View.GONE);
	}

	protected void updateCoins(String myCoins, String maxCoins) {
		try {
			mMyCoins.setText(myCoins);
			mMaxCoins.setText(maxCoins);
			mCoinProgressBar.setProgress(Integer.valueOf(myCoins));
		} catch (Exception e) {
			return;
		}
	}

	public void showFeedbackDialog() {
		FeedbackDialog mFeedbackDialog = new FeedbackDialog(this);
		mFeedbackDialog.show();
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.appQuestion:
			new GuideDialogFragment().show(getSupportFragmentManager(), "");
			break;
		case R.id.homeBanner:
			LoginBonusDialog mLoginBonusDialog = new LoginBonusDialog(this);
			mLoginBonusDialog.show();
			break;
		case R.id.myContentProgressBarCoins:
			Intent intent = new Intent(this, PointHistoryActivity.class);
			startActivity(intent);
			return;
			// case R.id.menuLeft:
			// currentView = R.id.menuLeft;
			// break;
			// case R.id.menuMiddle:
			// currentView = R.id.menuMiddle;
			// break;
			// case R.id.menuRight:
			// currentView = R.id.menuRight;
			// break;
		case R.id.naviMenuIcon:
			mNavigationDrawerFragment.openNaviDrawer();
			break;
		default:
			break;
		}
		// switchLayout(currentView);
	}

	@Override
	public void onPageScrollStateChanged(int arg0) {

	}

	@Override
	public void onPageScrolled(int arg0, float arg1, int arg2) {

	}

	@Override
	public void onPageSelected(int arg0) {

	}

	@Override
	public void onResume() {
		super.onResume();
		getUIDFromStore();
		getUserCoinsFromStore();
	}

	public int getUserCoins() {
		return mUserCoins;
	}

	protected void getUIDFromStore() {
		AdrewadsUtils.checkUId();
	}

	protected void getUserCoinsFromStore() {
		try {
			String coins = CommonModel.getInstance().getStore(
					Cache.ONE_TECH_USER_POINTS);
			if (coins != null) {
				mUserCoins = Integer.valueOf(coins);
			}
		} catch (Exception e) {
		}
	}

	public void getUserInfo() {
		if (mGetUserInfo == null || mGetUserInfo.getStatus() == Status.FINISHED) {
			mGetUserInfo = new GetUserInfo();
			mGetUserInfo.execute();
		}
	}

	private class GetUserInfo extends AsyncTask<Void, Void, UserInfoDTO> {
		@Override
		protected UserInfoDTO doInBackground(Void... params) {
			return UserModel.getInstance().getUserInfo(AppConfig.uId);
		}

		@Override
		protected void onPostExecute(UserInfoDTO result) {
			super.onPostExecute(result);

			try {
				if (result != null
						&& result.getTotal_coin().trim().length() > 0) {
					// AppConfig.userCoins=Integer.valueOf(result.getTotal_coin().trim());
					mUserCoins = Integer.valueOf(result.getTotal_coin().trim());

					updateCoins(result.getTotal_coin(),
							String.valueOf(AppConfig.MIN_COIN_EXCHANGE));
					// mCoinProgressBar.setProgress(Integer.valueOf(result.getTotal_coin()));
				}
			} catch (Exception e) {
				return;
			}
		}
	}

	public void go2HomePage() {
		try {
			Intent intent = new Intent(this, HomeActivity.class);
			intent.putExtra("m_id", "2835");
			intent.putExtra("m_owner_id", "558");
			if (AppConfig.uId != null && AppConfig.uId.trim().length() > 0) {

				intent.putExtra("user_id", AppConfig.uId.trim());
				intent.putExtra("uid", AppConfig.uId.trim());
			} else {
				intent.putExtra("user_id", "383cb3f413cc96b69cc78b818e433748");// account
																				// test
				intent.putExtra("uid", "383cb3f413cc96b69cc78b818e433748");// account
																			// test
			}
			intent.putExtra("platform_id", "0");
			intent.putExtra("url",
					"http://car.mobadme.jp/spg/spnew/558/2835/index.php");
			intent.putExtra("api_key", "76552d4faa4dd35b");
			intent.putExtra("app_key", "ncIdX3la");
			intent.putExtra("loading_msg", "loading..");
			intent.putExtra("timeout", "3000");

			// //

			intent.putExtra("appkey", "ncIdX3la");
			// //

			intent.putExtra("show_button", true);
			intent.putExtra("button_string", "back");
			intent.putExtra("bk_color", Color.BLUE);
			intent.putExtra("button_gravity", Gravity.RIGHT);

			startActivity(intent);
			this.finish();
		} catch (Exception e) {
			return;
		}
	}

	@Override
	public void onBackPressed() {
		// super.onBackPressed();
		try {
			ExitAppDialog mExitAppDialog = new ExitAppDialog(this, mHandler);
			mExitAppDialog.show();
		} catch (Exception e) {
		}
	}
	// end base functions
}
